package com.echo.yiwei.fiter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.echo.util.GsonUtil;
import com.echo.util.StringEchoUtil;
import com.echo.yiwei.contoller.ContollerSerlvet;

@WebFilter(filterName="fitler1" ,urlPatterns={"/userInterface/*","/productInterface/*"})
public class CommonFilter   implements  Filter{
	private static Logger  log= LoggerFactory.getLogger(ContollerSerlvet.class);

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
	           HttpServletRequest requestHTTP = (HttpServletRequest) request;
		log.info("请求路径：{}",requestHTTP.getRequestURI());  
		 Map<String,String> map=new HashMap<String,String>();
		/***
		 * 通过请求地址 区别业务处理的方法  
		 * 根据请求地址 解析成我们想要的响应的字符串
		 * 并放到请求参数中为我们在service 做业务判断
		 */
		 map.put("method", requestHTTP.getPathInfo().replace("/", ""));//请求方法
		 map.put("servicePath", requestHTTP.getServletPath().replace("/", ""));//本次请求归属模块
		 /***
		  * 获取json格式字符串输入流信息
		  */
		  String requestParmaes= StringEchoUtil.Inputstr2Str_Reader(requestHTTP.getInputStream(), "utf-8");
		  log.info("------请求参数为：{}",requestParmaes);
		  //使用传统的形式获取form请求方式
		  if(requestParmaes==null||requestParmaes.trim().length()<=2){
			  request.setCharacterEncoding("utf-8");
			  StringEchoUtil.getRequestParames(requestHTTP, map);
		  }
		  else{
			  //把请求字符流转换成Map形式的对象
		   map.putAll(GsonUtil.fromJson(requestParmaes, Map.class));
		  }
		  request.setAttribute("paramesMap", map);
		chain.doFilter(request, response);
		
		
		log.info("-------业务处理完成 组后一道坎！------");
	}

	public void destroy() {
		
	}
	public void init(FilterConfig filterConfig) throws ServletException {
	
	}

}
